﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ASBCC.Database.EntityFramework;

namespace ASBCC.Business.CIS
{
    public static class CustomerOCHelper
    {
        public static string GetOCString(int companyID)
        {
            using (var dc = new CCEntities())
            {
                var customers = dc.Customers.Where(p => p.Disabled == false && p.Company.CompanyID == companyID).ToList<Customer>();
                if (customers == null || customers.Count() == 0)
                    return "[]";
                string ocString = "";
                //获取Level1字符串
                string level1 = "[{v:'Level1', f:'LinkArea'}, '', 'Level 1'],";
                string level2 = "[{v:'Level2DepartmentID', f:'LinkArea'}, 'Level1', 'Level 2'],";
                string level3 = "[{v:'Level3DepartmentID', f:'LinkArea'}, 'Level2DepartmentID', 'Level 3'],";
                string level4 = "[{v:'Level4DepartmentID', f:'LinkArea'}, 'Level3DepartmentID', 'Level 4'],";
                string linkArea = "<a href=\"CustomerInfo.aspx?CustomerID=CILValue\" style=\"font-size:12px;\">CustomerName</a><br/>";
                var customersLevel1s = customers.Where(p => p.Level == 1);
                string linkArea1 = "";
                foreach (var customer in customersLevel1s)
                {
                    linkArea1 += linkArea.Replace("CILValue", customer.CustomerID.ToString()).Replace("CustomerName", customer.Name);
                }
                linkArea1 = linkArea1 == "" ? "<font style=\"font-size:12px;\">No Customer</font>" : linkArea1;
                ocString += level1.Replace("LinkArea", linkArea1);

                var departments = dc.CustomerDepartments.Where(p => p.Disabled == false).ToList<CustomerDepartment>();
                if (departments == null || departments.Count() == 0)
                    return "";
                foreach (var department in departments)
                {
                    var customerLevel2s = customers.Where(p => p.Disabled==false && p.CustomerDepartment!=null && p.CustomerDepartment.DepartmentID == department.DepartmentID && p.Level>1);
                    string linkArea2 = "";
                    string linkArea3 = "";
                    string linkArea4 = "";
                    foreach (var customer in customerLevel2s)
                    {
                        if (customer.Level == 2)
                            linkArea2 += linkArea.Replace("CILValue", customer.CustomerID.ToString()).Replace("CustomerName", customer.Name);
                        if (customer.Level == 3)
                            linkArea3 += linkArea.Replace("CILValue", customer.CustomerID.ToString()).Replace("CustomerName", customer.Name);
                        if (customer.Level == 4)
                            linkArea4 += linkArea.Replace("CILValue", customer.CustomerID.ToString()).Replace("CustomerName", customer.Name);
                    }
                    if (linkArea4 != "")
                    {
                        linkArea2 = linkArea2 == "" ? "<font style=\"font-size:12px;\">No Customer</font>" : linkArea2;
                        linkArea3 = linkArea3 == "" ? "<font style=\"font-size:12px;\">No Customer</font>" : linkArea3;
                        ocString += level2.Replace("LinkArea", linkArea2).Replace("DepartmentID", department.DepartmentID.ToString());
                        ocString += level3.Replace("LinkArea", linkArea3).Replace("DepartmentID", department.DepartmentID.ToString());
                        ocString += level4.Replace("LinkArea", linkArea4).Replace("DepartmentID", department.DepartmentID.ToString());
                    }
                    else if (linkArea3 != "")
                    {
                        linkArea2 = linkArea2 == "" ? "<font style=\"font-size:12px;\">No Customer</font>" : linkArea2;
                        ocString += level2.Replace("LinkArea", linkArea2).Replace("DepartmentID", department.DepartmentID.ToString());
                        ocString += level3.Replace("LinkArea", linkArea3).Replace("DepartmentID", department.DepartmentID.ToString());
                    }
                    else if (linkArea2 != "")
                        ocString += level2.Replace("LinkArea", linkArea2).Replace("DepartmentID", department.DepartmentID.ToString());
                }
                return "[" + ocString.Substring(0, ocString.Length - 1) + "]";
            }
        }
    }
}
